package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.R;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

//前台系统登录

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //Redis操作,把数据给缓存中
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 发短信
     *
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public R sendMsg(@RequestBody User user,
                     HttpSession session) {
        //获取手机号
        String userphone = user.getPhone();
        //随机生成验证码
        String code = ValidateCodeUtils.generateValidateCode(4).toString();
        //调用 sendMsg ,
        SMSUtils.sendMessage("antman", "SMS_136010026", userphone, code);
        System.out.println(code);
        //存入session中
        //需要将生成的验证码保存到Redis,设置过期时间
        redisTemplate.opsForValue().set(userphone, code.toString(), 5, TimeUnit.MINUTES);
//        session.setAttribute(userphone, code);
        return R.success("验证码发送成功");
    }

    /**
     * 登录
     *
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Map<String, String> map,
                   HttpSession session) {
        //收到验证码,手机号
        String phone = map.get("phone");
        String code = map.get("code");
        //获取真的验证码,进行比对
//        Object loginCode = session.getAttribute(phone);
        Object loginCode = redisTemplate.opsForValue().get(phone);

        if (loginCode != null && loginCode.equals(code)) {
            //对,则判断用户是否为新用户,是,添加User表中
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getPhone, phone);
            User one = userService.getOne(wrapper);

            if (one == null) {
                //新用户,注册(添加User表中)
                User user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            //登录成功
            session.setAttribute("user", one.getId());

            redisTemplate.delete(phone);

            return R.success(one);
        } else {
            //错误,则失败
            return R.error("验证码错误");
        }

    }


    /**
     * 退出登录
     * @param session
     * @return
     */
    @PostMapping("/loginout")
    public R userLoginout(HttpSession session){
        session.removeAttribute("user");
        return R.success("退出成功");
    }


}
